import TableSearch from '@/components/tableSearch'
import TableList from '@/components/standardTable'

export default {
    components: {
        TableSearch,
        TableList
    },
    data() {
        return {
            form: {
                page: 1,
                size: 10,
            },
            loading: false,
            selectionBox: [],
            list: [],
            total: 0,
            selectedRowKeys: [],
            scroll: {},
            timeout: null,
        }
    },
    computed: {
        pagination: function () {
            return { pageSize: this.form.size, current: this.form.page || 1, total: this.total || 0, onChange: this.pageChange }
        },
        selection: function () {
            return { selectedRowKeys: this.selectedRowKeys, onChange: this.onSelectChange }
        },
    },
    mounted() {
        // const handler = () => this.debounce(()=>this.search(1));
        // Object.keys(this.form).filter(item => !["current"].includes(item)).forEach(key => {
        //     this.$watch(vm => vm.form[key], handler, {
        //         deep: true
        //     });
        // });
    },
    created() {
        // this.setScrollWidth();
        // if(!this.standAlone){//是否需要独立调用 开始搜索页面
        //     this.$nextTick(()=>{
        //         this.debounce(()=>this.search(1),100,true)
        //     })
        // }
    },
    methods: {
        reset() {
            for (const key in this.originForm) {
                this.form[key] = this.originForm[key]
            }
            this.debounce(()=>this.search(1))
        },
        pageChange(current) {
            this.search(current)
            this.selectionBox = []
            this.selectedRowKeys = []
        },
        //多选
        onSelectChange(selectedRowKeys, selectedRows) {
            this.selectedRowKeys = selectedRowKeys;
            this.selectionBox = selectedRows;
        },
        //屏幕宽度
        setScrollWidth() {
            let width = document.body.clientWidth;
            let scrollWidth = width - 332;
            console.log(scrollWidth)
            this.scroll = {
                x: 1500
            }
        },
        //监听resize
        initResizeEvent() {
            window.addEventListener('resize', this.setScrollWidth);
        },
        //移除resize
        destroyResizeEvent() {
            window.removeEventListener('resize', this.setScrollWidth);
        },
        debounce(func, wait = 500, immediate = false) {
            // 清除定时器
            if (this.timeout !== null) clearTimeout(this.timeout);
            // 立即执行，此类情况一般用不到
            if (immediate) {
                var callNow = !this.timeout;
                this.timeout = setTimeout(function () {
                    this.timeout = null;
                }, wait);
                if (callNow) typeof func === 'function' && func();
            } else {
                // 设置定时器，当最后一次操作后，timeout不会再被清除，所以在延时wait毫秒后执行func回调方法
                this.timeout = setTimeout(function () {
                    typeof func === 'function' && func();
                }, wait);
            }
        }
    },
}
